<template>
  <div class="home">
    <swiper ref="mySwiper" :options="swiperOptions">
      <swiper-slide>
        <img src="../assets/img/banner_1.jpg" class="w-100" />
      </swiper-slide>
      <swiper-slide>
        <img src="../assets/img/banner_2.jpg" class="w-100" />
      </swiper-slide>
      <swiper-slide>
        <img src="../assets/img/banner_3.jpg" class="w-100" />
      </swiper-slide>
      <div class="swiper-pagination pagination-home text-right px-3 pb-1" slot="pagination"></div>
    </swiper>
    <!-- swiper end -->
    <div class="nav-icons bg-white mt-3 text-center pt-3 text-dark-1">
      <div class="d-flex flex-wrap">
        <div class="nav-item mb-3">
          <i class="sprite sprite-news"></i>
          <div class="py-2">爆料站</div>
        </div>
        <div class="nav-item mb-3">
          <i class="sprite sprite-story"></i>
          <div class="py-2">故事站</div>
        </div>
        <div class="nav-item mb-3">
          <i class="sprite sprite-mall"></i>
          <div class="py-2">周边商城</div>
        </div>
        <div class="nav-item mb-3">
          <i class="sprite sprite-experience"></i>
          <div class="py-2">体验服</div>
        </div>
        <div class="nav-item mb-3">
          <i class="sprite sprite-newbie"></i>
          <div class="py-2">新人专区</div>
        </div>
        <div class="nav-item mb-3">
          <i class="sprite sprite-smriti"></i>
          <div class="py-2">
            荣耀
            <b>·</b>传承
          </div>
        </div>
        <div class="nav-item mb-3">
          <i class="sprite sprite-community"></i>
          <div class="py-2">同人社区</div>
        </div>
        <div class="nav-item mb-3">
          <i class="sprite sprite-campsite"></i>
          <div class="py-2">王者营地</div>
        </div>
      </div>
      <div class="bg-light py-2 fs-sm">
        <i class="sprite sprite-arrow mr-1"></i>
        <span class="text-gray">收起</span>
      </div>
    </div>
    <!-- icons end -->

    <m-list-card icon="caidan1" title="新闻资讯" :categories="newsCats">
      <template #items="{category}">
        <router-link tag="div" :to="`/articles/${item._id}`" class="py-2 fs-lg d-flex" v-for="(item,i) in category.newsList" :key="i">
          <span class="text-info">[{{item.categoryName}}]</span>
          <span class="px-2">|</span>
          <span class="flex-1 text-dark-1 text-ellipsis pr-2">{{item.title}}</span>
          <span class="text-grey-1">{{item.createdAt | date}}</span>
        </router-link>
      </template>
    </m-list-card>

    <m-list-card icon="card-hero" title="英雄列表" :categories="heroCats">
      <template #items="{category}">
        <div class="d-flex flex-wrap" style="margin:0 -0.8rem">
          <router-link :to="`/heroes/${hero._id}`" tag="div" class="p-2 text-center" style="width:20%"
          v-for="(hero,i) in category.heroList" :key="i">
          <img :src="hero.avatar" class="w-100">
          <div>{{hero.name}}</div>
        </router-link>
        </div>
      </template>
    </m-list-card>
  </div>
</template>

<script>
import dayjs from 'dayjs'
export default {
  filters:{
    date(val){
      return dayjs(val).format('MM/DD')
    }
  },
  data() {
    return {
      newsCats: [],
      heroCats:[],
      swiperOptions: {
        pagination: {
          el: ".pagination-home"
        },
        // Some Swiper option/callback...
        observer: true, //修改swiper自己或子元素时，自动初始化swiper
        observeParents: true, //修改swiper的父元素时，自动初始化swiper
        loop: true,
        autoplay: {
          delay: 2000,
          disableOnInteraction: false
        }
      }
    };
  },
  methods:{
    async fetchNewsCats(){
      const res = await this.$http.get('/news/list')
      this.newsCats = res.data
    },
    async fetchHeroesCats(){
      const res = await this.$http.get('/heroes/list')
      this.heroCats = res.data
    }
  },
  created(){
    this.fetchNewsCats();
    this.fetchHeroesCats()
  },
  computed: {
    swiper() {
      return this.$refs.mySwiper.$swiper;
    }
  },
  mounted() {}
};
</script>

<style lang="scss">
@import "../assets/scss/variables";
.pagination-home {
  .swiper-pagination-bullet {
    opacity: 1;
    border-radius: 0.1538rem;
    background-color: map-get($colors, "white");
    &.swiper-pagination-bullet-active {
      background-color: map-get($colors, "info");
    }
  }
}
.nav-icons {
  border-top: 1px solid $border-color;
  border-bottom: 1px solid $border-color;
  .nav-item {
    width: 25%;
    border-left: 1px solid $border-color;
    &:nth-child(4n + 1) {
      border: 0;
    }
  }
}
</style>
